﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EZCRS.HCMS.Data;
using EZCRS.HCMS.Entity;
using System.Data;
using System.Data.SqlClient;

namespace EZCRS.HCMS.SqlClient
{
    public class sql_AdmScoreLog
    {
        string sqlstr = "";
        string searchs = "";

        /// <summary>
        /// 返回查询的积分日志
        /// </summary>
        /// <param name="cpage">页数</param>
        /// <param name="bstart">开始时间</param>
        /// <param name="bend">结束时间</param>
        /// <param name="logsort">积分类型</param>
        /// <param name="valid">有效否</param>
        /// <returns></returns>
        public DataSet scoreloglist(string gid, string uid, int cpage, string bstart, string bend, string logsort, string valid)
        {
            #region
            searchs = " gid=" + gid + " and uid=" + uid + " and valid=" + valid;

            if (bstart != "")
                searchs += " and scoredate>'" + bstart + "'";
            if (bend != "")
                searchs += " and scoredate<'" + bend + "'";
            if (logsort != "")
                searchs += " and logsort=" + logsort;
            return ExecSql.AddParamFrKeyField("hfg_scorelog", "*", " scoredate desc", "logid", CFun.PageSize, cpage, searchs);
            #endregion
        }

        /// <summary>
        /// 修改积分日志状态
        /// </summary>
        /// <param name="logid">要修改的id列</param>
        /// <returns></returns>
        public bool scorelogstatus(string logid)
        {
            #region
            sqlstr = "UPDATE hfg_scorelog SET valid = CASE valid WHEN 1 THEN 0 ELSE 1 END  where logid in (" + logid + ")";
            if (ExecSql.SqlExecNoquery(sqlstr) > 0)
                return true;
            else
                return false;
            #endregion
        }

        /// <summary>
        /// 买积分
        /// </summary>
        /// <param name="gid"></param>
        /// <param name="uid"></param>
        /// <param name="scorerate"></param>
        /// <param name="totalmoney"></param>
        /// <param name="totalscore"></param>
        /// <param name="admin"></param>
        /// <returns></returns>
        public decimal scorebuy(string gid, string uid, string totalmoney, string admin)
        {
            #region
            decimal rst = 0;
            SqlParameter[] pam = new SqlParameter[5];
            pam[0] = new SqlParameter("@gid", gid);
            pam[1] = new SqlParameter("@uid", uid);
            pam[2] = new SqlParameter("@totalmoney", totalmoney);
            pam[3] = new SqlParameter("@admin", admin);
            pam[4] = new SqlParameter("@result", rst);
            pam[4].Direction = ParameterDirection.Output;

            SqlCommand outcmd = null;

            ExecSql.SqlExecNoqueryByProcedure("hfg_buyscore", out outcmd, pam);
            if (outcmd != null)
            {
                return Convert.ToDecimal(outcmd.Parameters["@result"].Value);
            }
            else
                return 0;
            #endregion
        }
    }
}
